# A tibble: 26 × 3
ID Timepoint Antibody_level
<fct> <chr> <int>
1 1 Before 105
2 1 After 85
3 2 Before 50
4 2 After 74
5 3 Before 136
6 3 After 145
7 4 Before 90
8 4 After 86
9 5 Before 122
10 5 After 148
# ℹ 16 more rows
Plotting reaction norms
group = ID tells geom_line() to associate lines with IDs.
ggplot(BB_long, aes(x = Timepoint, y = Antibody_level, group = ID)) +geom_point() +geom_line()
Plotting reaction norms
Ordering factors
Also many options in the forcats package (fct_ functions) when you already have a factor.
BB_long <- BB_long |>mutate(Timepoint =factor(Timepoint, levels =c("Before", "After")))ggplot(BB_long, aes(x = Timepoint, y = Antibody_level, group = ID)) +geom_point() +geom_line()
Ordering factors
Adding a mean and error bars
stat_summary() can add the output of summary functions.
mean_cl_boot give the mean and bootstrapped 95% CI. (requires Hmisc)
aes(group = -1) ungroups the data so that the statistics will calculate for each Timepoint
ggplot(BB_long, aes(x = Timepoint, y = Antibody_level, group = ID)) +geom_point() +geom_line() +stat_summary(aes(group =-1),fun.data ="mean_cl_boot",color ="magenta",size =1)
Adding a mean and error bars
Horizontal, Vertical, and ab lines
ggplot() +geom_hline(yintercept =-5:5, color ="orangered") +geom_vline(xintercept =-5:5, color ="orangered") +geom_abline(slope =seq(1, 10, by =0.5), intercept =0)
Horizontal, Vertical, and ab lines
Horizontal, Vertical, and ab lines: refined
ggplot() +geom_hline(yintercept =-5:5, color ="orangered") +geom_vline(xintercept =-5:5, color ="orangered") +geom_abline(slope =seq(1, 10, by =0.5), intercept =0) +coord_equal()
Horizontal, Vertical, and ab lines: refined
Adding regression lines
geom_smooth()
Can handle a range of models from lm() to GLM and GAM
Splits by aesthetics automatically
Compute regression and use predict() or another helper function
You handle all the prediction
Necessary for more complex models (e.g., mixed/multilevel)
Bird species richness
Bird species richness in different habitat patches sampled in Jamaica.1
Defaults to a loess smoother with confidence interval
ggplot(BSR, aes(x = log_Area, y = n_Species, color = Landscape_type)) +geom_point() +geom_smooth()
geom_smooth()
geom_smooth(formula = y ~ x, method = "lm")
“Standard” linear regression
ggplot(BSR, aes(x = log_Area, y = n_Species, color = Landscape_type)) +geom_point() +geom_smooth(formula = y ~ x, method ="lm")
geom_smooth(formula = y ~ x, method = "lm")
Removing the confidence interval
se = FALSE
p1 <-ggplot(BSR, aes(x = log_Area, y = n_Species, color = Landscape_type)) +geom_point() +geom_smooth(formula = y ~ x, method ="lm", se =FALSE)p1
Removing the confidence interval
Removing the grouping
One line through all the points: aes(group = -1)
ggplot(BSR, aes(x = log_Area, y = n_Species, color = Landscape_type)) +geom_point() +geom_smooth(aes(group =-1), formula = y ~ x, method ="lm", se =FALSE)
Removing the grouping
“Manual” prediction
fm <-lm(n_Species ~ log_Area + Landscape_type, data = BSR)summary(fm)
Slope for log_Area common to all levels of Landscape_type
Separate intercepts for each Landscape_type
“Manual” prediction
Call:
lm(formula = n_Species ~ log_Area + Landscape_type, data = BSR)
Residuals:
Min 1Q Median 3Q Max
-18.5508 -2.9911 0.2049 3.1838 10.7961
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 22.270 1.240 17.958 < 2e-16 ***
log_Area 12.271 1.243 9.869 5.35e-16 ***
Landscape_typeBauxite -5.351 1.455 -3.677 0.000401 ***
Landscape_typeForest -2.151 1.321 -1.629 0.106855
Landscape_typeUrban -5.506 1.488 -3.701 0.000369 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 4.84 on 90 degrees of freedom
Multiple R-squared: 0.6043, Adjusted R-squared: 0.5867
F-statistic: 34.36 on 4 and 90 DF, p-value: < 2.2e-16
predict()
predict() has methods for many regression-like functions:
predict.lm()
predict.glm()
etc.
Type predict. and then Tab to see options
By default returns the predicted values for observed data.